/**
 * 每隔一秒计算系统运行累计时间，并更新页面值
 */
function accumulative() {
	window.currentTimestamp += 1000;
	let dateObj = timeDifference(window.systemOnlineTimestamp, window.currentTimestamp);
	document.getElementById("y1").innerText = dateObj.differenceYear;
	document.getElementById("d1").innerText = dateObj.differenceDay;
	document.getElementById("h1").innerText = dateObj.hour;
	document.getElementById("i1").innerText = dateObj.minute;
	document.getElementById("s1").innerText = dateObj.second;
}

/**
 * 计算时间差
 * @param {Object} systemOnlineTimestamp
 * @param {Object} currentTimestamp
 */
function timeDifference(systemOnlineTimestamp, currentTimestamp) {
	//上线时间
	var onlineDate = new Date(systemOnlineTimestamp);
	var onlineYear = onlineDate.getFullYear();
	var onlineMonth = onlineDate.getMonth() + 1;
	var onlineDay = onlineDate.getDate();
	var onlineMiao = onlineDate.getTime();
	//服务器当前时间
	var currentDate = new Date(currentTimestamp);
	var currentYear = currentDate.getFullYear();
	var currentMonth = currentDate.getMonth() + 1;
	var currentMiao = currentDate.getTime();
	//相差时间
	var differTime = currentMiao - onlineMiao;

	var differDay = parseInt(differTime / 86400000);
	var differDayYu = differTime % 86400000;

	var differHour = parseInt(differDayYu / 3600000);
	var differHourYu = differDayYu % 3600000;

	var differMin = parseInt(differHourYu / 60000);
	var differMinYu = differHourYu % 60000;

	var differSec = parseInt(differMinYu / 1000);

	var differenceYear = 0,
		differenceDay = 0;
	if (currentYear == onlineYear) {
		differenceDay = differDay
	} else {
		for (let x = 0; x <= currentYear - onlineYear; x++) {
			var thatYear = onlineYear + x;
			var thatTime = new Date(thatYear + '/' + onlineMonth + '/' + onlineDay).getTime();
			var tempDay = parseInt((thatTime - onlineMiao) / 86400000);
			if (tempDay > 0 && tempDay <= differDay) {
				differenceYear = differenceYear + 1;
				differenceDay = differDay - tempDay
			}
		}
	}

	return {
		differenceYear: differenceYear,
		differenceDay: differenceDay,
		day: differDay,
		hour: differHour,
		minute: differMin,
		second: differSec
	}
}

/**
 * 获取一年的天数
 * @param {Object} t
 */
function yearOfDays(t) {
	if (t == null) {
		var Year = new Date().getFullYear(),
			s = 0,
			d; //获取当前年
		for (var i = 1; i < 13; i++) {
			//获取某一个月的天数
			d = new Date(Year, i, 0);
			s += d.getDate();
		};
		return s;
	} else if (t >= 1970) {
		var Year = new Date().getFullYear(),
			s = 0,
			d;
		for (var i = 1; i < 13; i++) {
			d = new Date(t, i, 0);
			s += d.getDate();
		};
		return s;
	} else {
		return "error input year";
	}
}
